package cn.com.dycc.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public final class ImageUtil {
	
	
	
	// 验证码字符集
	private static final char[] chars = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	// 字符数量
	private static final int SIZE = 4;
	// 干扰线数量
	private static final int LINES = 5;
	// 宽度
	private static final int WIDTH = 96;
	// 高度
	private static final int HEIGHT = 29;
	// 字体大小
	private static final int FONT_SIZE = 30;

	
	/**
	 * 返回图片的尺寸
	 * @param file 图片文件
	 * @return int[]数组，索引号0:width;索引号1:height
	 * @throws IOException
	 */
	public static int[] getImageSize(File file) throws IOException{
		
		if(file==null||!file.exists()){
			return null;
		}
		int[] size=new int[2];
		BufferedImage bufferedImage=ImageIO.read(file);
		int width=bufferedImage.getWidth();
		int height=bufferedImage.getHeight();
		size[0]=width;
		size[1]=height;
		return size;
		
	}
	
	
	
	
	/**
	 * 生成随机验证码及图片
	 */
	public static Object[] createImage() {
		StringBuffer sb = new StringBuffer();
		// 1.创建空白图片
		BufferedImage image = new BufferedImage(
			WIDTH, HEIGHT, BufferedImage.TYPE_3BYTE_BGR);
		// 2.获取图片画笔
		Graphics graphic = image.getGraphics();
		// 3.设置画笔颜色
		graphic.setColor(Color.LIGHT_GRAY);
		// 4.绘制矩形背景
		graphic.fillRect(0, 0, WIDTH, HEIGHT);
		// 5.画随机字符
		Random ran = new Random();
		for (int i = 0; i <SIZE; i++) {
			// 取随机字符索引
			int n = ran.nextInt(chars.length);
			// 设置随机颜色
			
			graphic.setColor(new Color(16,28,165));
			
			// 设置字体大小
			graphic.setFont(new Font(
				null, Font.BOLD + Font.ITALIC, FONT_SIZE));
			// 画字符
			graphic.drawString(chars[n] + "", i * WIDTH / SIZE, HEIGHT);
			// 记录字符
			sb.append(chars[n]);
		}
		// 6.画干扰线
		for (int i = 0; i < LINES; i++) {
			// 设置随机颜色
			graphic.setColor(getRandomColor());
			// 随机画线
			graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
					ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
		}
		// 7.返回验证码和图片
		return new Object[]{sb.toString(), image};
	}

	/**
	 * 随机取色
	 */
	public static Color getRandomColor() {
		Random ran = new Random();
		Color color = new Color(ran.nextInt(256), 
				ran.nextInt(256), ran.nextInt(256));
		return color;
	}
	
	public static void main(String[] args) throws IOException {
		Object[] objs = createImage();
		BufferedImage image = 
			(BufferedImage) objs[1];
		//home/soft01/1.png
		String num = (String) objs[0];
//		System.out.println(num);
		OutputStream os = 
			new FileOutputStream("D:/1.jpg");
		ImageIO.write(image, "png", os);
		os.close();
	}
	
	public static byte[] imgToByteArray(BufferedImage img) throws IOException{
		// 1.创建图片对象
		//BufferedImage img = (BufferedImage) objs[1];
		// 2.将图片编码为png
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		ImageIO.write(img, "png", out);
		
		out.close();
		// 3.得到所有的bytes
		byte[] ary = out.toByteArray();
		return ary;
	}
	
	
//	// 图片转化成base64字符串
//	public static String encodeImageToStr(InputStream file) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
//	byte[] data = null;
//	// 读取图片字节数组
//	try {
//	data = new byte[file.available()];
//	file.read(data);
//	file.close();
//	} catch (IOException e) {
//	e.printStackTrace();
//	}
//	// 对字节数组Base64编码
//	BASE64Encoder encoder = new BASE64Encoder();
//	return encoder.encode(data);// 返回Base64编码过的字节数组字符串
//	}
}
